const express = require('express')
const router = express.Router()
const Http = require('../db/model/HttpModel')
const { mySend, Vtoken, checkParams } = require('../utils/index')
const { to } = require('await-to-js')

router.post('/add', async (req, res) => {
    const { url, mark, remarks, phone, notifyStatus, status, token } = req.body
    let checked = checkParams({ url, mark, token })
    if (checked) return mySend(res, 1, checked)
    let user = await Vtoken(token)
    if (!user) return mySend(res, 314, 'token不存在或已过期')

    let data = {
        url,
        userId: user._id,
        username: user.username,
        mark,
    }
    if (phone) {
        if (!(/^1[3456789]\d{9}$/.test(phone))) {
            return mySend(res, 1, '手机号不正确')
        }
        data.phone = phone;
        data.notifyStatus = notifyStatus
    }

    if(status){
        data.status = status
    }

    if(remarks){
        data.remarks = remarks
    }

    let [err, result] = await to(Http.insertMany(data))
    if(err) return mySend(res, 1, '添加失败', err)
    return mySend(res, 0, '添加成功', result)
})

router.post('/delete', async (req, res) => {
    const { _id, token } = req.body
    let checked = checkParams({ _id, token })
    if (checked) return mySend(res, 1, checked)
    let user = await Vtoken(token)
    if (!user) return mySend(res, 314, 'token不存在或已过期')

    let [err, result] = await to(Http.deleteOne({_id}))
    if(err) return mySend(res, 1, '删除失败', err)
    if(result.deletedCount === 0) return mySend(res, 1, '删除失败', result)
    return mySend(res, 0, '删除成功', result)
})

router.post('/update', async (req, res) => {
    const {token, _id} = req.body
    let checked = checkParams({ _id})
    if (checked) return mySend(res, 1, checked)
    let user = await Vtoken(token)
    if (!user) return mySend(res, 314, 'token不存在或已过期')

    let [err, result] = await to(Http.updateOne({_id: req.body._id}, {...req.body}))
    if(err) return mySend(res, 1, '修改失败', err)
    if(result.nModified === 0) return mySend(res, 1, '你没有修改任何数据?', result)
    return mySend(res, 0, '修改成功', result)
})

router.post('/list', async (req, res) => {
    const {token, current = 1, size = 10, status, urlStatus, notifyStatus} = req.body
    let user = await Vtoken(token)
    if (!user) return mySend(res, 314, 'token不存在或已过期')

    let query = {userId: user._id}
    if(status) query.status = status
    if(urlStatus) query.urlStatus = urlStatus
    if(notifyStatus) query.notifyStatus = notifyStatus

    let [err, total] = await to(Http.countDocuments(query))
    let [err1, result] = await to(Http.find(query).sort({ctime: -1}).skip((current - 1) * size).limit(size))
    if(err1) return mySend(res, 1, '查询失败', err1)
    let body = {
        total,
        current, 
        size,
        list: result
    }
    return mySend(res, 0, 'success', body)
})

router.post('/listOne', async (req, res) => {
    const {token, _id} = req.body
    let checked = checkParams({ _id})
    if (checked) return mySend(res, 1, checked)
    let user = await Vtoken(token)
    if (!user) return mySend(res, 314, 'token不存在或已过期')

    let [err, result] = await to(Http.find({_id, userId: user._id}))
    if(err) return mySend(res, 1, '查询失败', err)
    return mySend(res, 0, 'success', result[0])
})

//不传userId, 查询所有用户,传userId查指定用户
router.post('/listAll', async (req, res) => {
    const {token, userId, current = 1, size = 10} = req.body
    let user = await Vtoken(token)
    if (!user) return mySend(res, 314, 'token不存在或已过期')
    if(user.role !== 1) return mySend(res, 1, '你没有权限查看')

    let query = {}
    if(userId) query.userId = userId
    let [err, total] = await to(Http.countDocuments({query}))
    let [err1, result] = await to(Http.find(query).sort({userId: -1, ctime: -1}).skip((current - 1) * size).limit(size))
    if(err) return mySend(res, 1, '查询失败', err)
    let body = {
        total,
        current, 
        size,
        list: result
    }
    return mySend(res, 0, 'success', body)
})

module.exports = router

